package com.youlai.boot.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.youlai.boot.system.converter.DictItemConverter;
import com.youlai.boot.system.mapper.DictDataMapper;
import com.youlai.boot.system.model.entity.SysDictItemEntity;
import com.youlai.boot.system.model.form.DictItemForm;
import com.youlai.boot.system.model.query.DictItemPageQuery;
import com.youlai.boot.system.model.vo.DictItemOptionVO;
import com.youlai.boot.system.model.vo.DictItemPageVO;
import com.youlai.boot.system.service.DictItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 字典项实现类
 *
 * @author Ray.Hao
 * @since 2022/10/12
 */
@Service
@RequiredArgsConstructor
public class DictItemServiceImpl extends ServiceImpl<DictDataMapper, SysDictItemEntity> implements DictItemService {

    private final DictItemConverter dictItemConverter;

    /**
     * 获取字典项分页列表
     *
     * @param queryParams /
     * @return /
     */
    @Override
    public Page<DictItemPageVO> getDictItemPage(DictItemPageQuery queryParams) {
        Page<DictItemPageVO> page = new Page<>(queryParams.getPageNum(), queryParams.getPageSize());

        QueryWrapper query = QueryWrapper.create()
                .from(SysDictItemEntity.class)
                .and(SysDictItemEntity::getDictCode)
                .eq(queryParams.getDictCode(), StrUtil.isNotBlank(queryParams.getDictCode()));

        if (StrUtil.isNotBlank(queryParams.getKeywords())) {
            query.and(q -> {
                q.and(SysDictItemEntity::getValue).like(queryParams.getKeywords())
                        .or(SysDictItemEntity::getLabel).like(queryParams.getKeywords());
            });
        }

        return this.pageAs(
                page,
                query,
                DictItemPageVO.class
        );
    }

    /**
     * 获取字典项列表
     *
     * @param dictCode 字典编码
     * @return 字典项列表
     */
    @Override
    public List<DictItemOptionVO> getDictItems(String dictCode) {
        return this.list(
                        QueryWrapper.create().from(SysDictItemEntity.class)
                                .select(
                                        QueryMethods.column(SysDictItemEntity::getValue),
                                        QueryMethods.column(SysDictItemEntity::getLabel),
                                        QueryMethods.column(SysDictItemEntity::getTagType)
                                )
                                .eq(SysDictItemEntity::getDictCode, dictCode)
                                .eq(SysDictItemEntity::getStatus, 1)
                                .orderBy(SysDictItemEntity::getSort).asc()
                ).stream().map(item -> new DictItemOptionVO(item.getValue(), item.getLabel(), item.getTagType()))
                .toList();
    }

    /**
     * 获取字典项表单
     *
     * @param id 字典项ID
     * @return 字典项表单
     */
    @Override
    public DictItemForm getDictItemForm(Long id) {
        SysDictItemEntity entity = this.getById(id);
        return dictItemConverter.toForm(entity);
    }

    /**
     * 新增字典项
     *
     * @param formData 字典项表单
     * @return 是否成功
     */
    @Override
    public boolean saveDictItem(DictItemForm formData) {
        SysDictItemEntity entity = dictItemConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新字典项
     *
     * @param formData 字典项表单
     * @return 是否成功
     */
    @Override
    public boolean updateDictItem(DictItemForm formData) {
        SysDictItemEntity entity = dictItemConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除字典项
     *
     * @param ids 字典项ID集合
     */
    @Override
    public void deleteDictItemByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).toList();
        this.removeByIds(idList);
    }


}




